package com.bhdx.blog.controller.admin;

/**
 * @author bhdx
 * @date 2020/6/18-14:26
 */

import com.alibaba.fastjson.JSONObject;
import com.bhdx.blog.entity.Blog;
import com.bhdx.blog.entity.BlogType;
import com.bhdx.blog.entity.Blogger;
import com.bhdx.blog.entity.Link;
import com.bhdx.blog.service.BlogService;
import com.bhdx.blog.service.BlogTypeService;
import com.bhdx.blog.service.BloggerService;
import com.bhdx.blog.service.LinkService;
import com.bhdx.blog.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContextUtils;


import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping(value = "/admin")
public class MainAdminController {
    @Resource
    private BloggerService bloggerService;
    @Resource
    private LinkService linkService;
    @Resource
    private BlogTypeService blogTypeService;
    @Resource
    private BlogService blogService;

    // 刷新系统缓存
    @RequestMapping("/refreshSystemCache")
    public String refreshSystemCache(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ServletContext application = RequestContextUtils.findWebApplicationContext(request).getServletContext();


        // 获取博主信息
        Blogger blogger = bloggerService.getBloggerData();
        blogger.setPassword(null);
        application.setAttribute("blogger", blogger);

        // 获取友情链接信息
        List<Link> linkList = linkService.getTotalData();
        application.setAttribute("linkList", linkList);

        // 获取博客类别信息
        List<BlogType> blogTypeList = blogTypeService.getBlogTypeData();
        application.setAttribute("blogTypeList", blogTypeList);

        // 获取博客信息，按照时间分类的
        List<Blog> blogTimeList = blogService.getBlogData();
        application.setAttribute("blogTimeList", blogTimeList);

        JSONObject result = new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }

//    @RequestMapping(value = "/menu")
//    public String toMenuPage(){
//        return "admin/main";
//    }
//
//    @RequestMapping(value = "/menu1")
//    public String toMenuPage1(){
//        return "admin/main1";
//    }
}

